package com.guigu.controller;

import com.guigu.entity.MemberBean;
import com.guigu.entity.OrderBean;
import com.guigu.entity.OrdersettingBean;
import com.guigu.service.OrderService;
import com.guigu.service.OrdersettingService;
import com.guigu.vo.PackageVO;
import com.guigu.vo.Result;
import com.guigu.vo.SubscribeListVO;
import com.guigu.vo.TjyyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService rderService;

    @Autowired
    private OrdersettingService ordersettingService;

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/findAll")
    public Result<List<SubscribeListVO>> findAll() {


        List<SubscribeListVO> SubscribeListVOList = this.rderService.selectOrder();

        System.out.println("预约结果集："+SubscribeListVOList);

        return new Result<>("200", "true", "成功查询用户列表", SubscribeListVOList);
    }

    @GetMapping("/tcfindAll")
    public Result<List<PackageVO>> tcfindAll() {


        List<PackageVO> PackageVOList = this.rderService.selectPackage();

        System.out.println("tc结果集："+PackageVOList);

        return new Result<>("200", "true", "成功查询用户列表", PackageVOList);
    }
    @PostMapping("/tjyy")
    public Result tjyy(@RequestBody TjyyVO tjyyVO) {
        // 处理 POST 请求的逻辑代码
        System.out.println("添加中的form===="+tjyyVO);

        MemberBean hyxx=this.rderService.gjxmsfzcxhyxx(tjyyVO.getName(),tjyyVO.getIdcard());

        System.out.println("查询的会员信息===="+hyxx);

        if(hyxx==null){


            return new Result<>("200", "true", "添加成功", "会员不存在");

        }else{

            OrderBean orderBean=new OrderBean(0,hyxx.getId(),tjyyVO.getDate2(),tjyyVO.getOrdertype(),"已预约",
                    tjyyVO.getPackageid());
            System.out.println("添加预约信息===="+orderBean);
            this.rderService.addOrder(orderBean);
            return new Result<>("200", "true", "添加成功", "添加成功");

        }

    }

    @PostMapping("/xgyyzt")
    public Result xgyyzt(@RequestBody TjyyVO tjyyVO) {
        // 处理 POST 请求的逻辑代码
        System.out.println("tjyyVO====" + tjyyVO);
        OrderBean orderBean = new OrderBean();
        orderBean.setId(tjyyVO.getId());
        orderBean.setOrderStatus(tjyyVO.getOrderstatus());
        System.out.println("修改状态orderBean====" + orderBean);


        // 设置时区为中国北京
        TimeZone chinaTimeZone = TimeZone.getTimeZone("Asia/Shanghai");

        // 获取中国当前日期
        LocalDateTime currentDate = LocalDateTime.now(ZoneId.of(chinaTimeZone.getID()));
        Date datecurrentDate = Date.from(currentDate.atZone(ZoneId.of(chinaTimeZone.getID())).toInstant());

        OrdersettingBean ordersettingBean=new OrdersettingBean(
                0,
                tjyyVO.getDate2(),
                0,
                0
        );

        System.out.println("ordersettingBean====" + ordersettingBean);
        System.out.println("日期搜索条件====" + ordersettingBean.getOrderDate());

        OrdersettingBean cxdyysz=this.ordersettingService.selectOrdersettingByorderDate(ordersettingBean);

        System.out.println("查询的预约设置：" + cxdyysz); // 输出格式化后的日期时间

        if(cxdyysz==null){

            return new Result<>("200", "true", "添加成功", "未设置可预约人数");


        }else{
            if(cxdyysz.getReservations()>cxdyysz.getReservations()){
                return new Result<>("200", "true", "添加成功", "可预约人数已上限");

            }else{
                ordersettingService.updatereservations(ordersettingBean);
                this.rderService.updateorderStatus(orderBean);

            }

        }



        return new Result<>("200", "true", "操作成功", "操作成功");
    }
}
